package tbalert.web.manager.impl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import tbalert.hibernate3.Finder;
import tbalert.hibernate3.Updater;
import tbalert.page.Pagination;
import tbalert.security.BadCredentialsException;
import tbalert.security.UsernameNotFoundException;
import tbalert.session.SessionProvider;
import tbalert.web.PwdEncoder;
import tbalert.web.SysConstants;
import tbalert.web.dao.DwdmDao;
import tbalert.web.entity.Dwdm;
import tbalert.web.manager.DwdmMng;

@Transactional
public class DwdmMngImpl implements DwdmMng {

	@Autowired(required = true)
	private PwdEncoder pwdEncoder;

	@Autowired(required = true)
	private DwdmDao dao;

	@Override
	public String buildDwid(String sjdwId) {
		if (StringUtils.equals(sjdwId, "00")) {
			sjdwId = "";
		}
		String maxDwid = dao.getMaxDwid(sjdwId);
		if (maxDwid == null) {
			return sjdwId + "01";
		}
		int suffix = NumberUtils.toInt(StringUtils.right(maxDwid, 2));
		if (suffix < 99) {
			suffix++;
			if (suffix < 10) {
				return StringUtils.left(maxDwid, maxDwid.length() - 2) + "0" + suffix;
			} else {
				return StringUtils.left(maxDwid, maxDwid.length() - 2) + suffix;
			}
		} else {
			return null;
		}
	}

	public Dwdm disable(String id) {
		Dwdm bean = dao.findById(id);
		bean.setDisabled(true);
		dao.updateByUpdater(new Updater<Dwdm>(bean));
		return bean;
	}

	public boolean dlmcExist(String dlmc) {
		return getByDlmc(dlmc) != null;
	}

	public Dwdm enable(String id) {
		Dwdm bean = dao.findById(id);
		bean.setDisabled(false);
		dao.updateByUpdater(new Updater<Dwdm>(bean));
		return bean;
	}

	@Transactional(readOnly = true)
	public List<Dwdm> findBySjdw(String sjdwId) {
		return dao.findBySjdw(sjdwId);
	}

	@Transactional(readOnly = true)
	public Pagination find(Dwdm dwdm, int pageNo, int pageSize) {
		String sjdwId = dwdm.getDwid();
		if (StringUtils.equals(sjdwId, "00")) {
			sjdwId = "";
		}
		Finder finder = Finder.create("from Dwdm where dwid like '" + sjdwId + "%' and dwid<>'bssa'");
		if (dwdm.getRole() != null && dwdm.getRole().getId() != null) {
			finder.append(" and role=" + dwdm.getRole().getId());
		}
		if (!StringUtils.isBlank(dwdm.getDwmc())) {
			finder.append(" and dwmc like '%" + dwdm.getDwmc() + "%'");
		}
		if (!StringUtils.isBlank(dwdm.getDlmc())) {
			finder.append(" and dlmc like '%" + dwdm.getDlmc() + "%'");
		}
		if (dwdm.getDisabled() != null) {
			finder.append(" and disabled =" + dwdm.getDisabled().compareTo(false));
		}
		finder.append(" order by dwid");
		return dao.find(finder, pageNo, pageSize);
	}

	@Transactional(readOnly = true)
	public Dwdm findById(String id) {
		return dao.findById(id);
	}

	@Transactional(readOnly = true)
	public Dwdm getByDlmc(String dlmc) {
		return dao.getByDlmc(dlmc);
	}

	public boolean isDlmmValid(String id, String dlmm) {
		Dwdm bean = findById(id);
		return pwdEncoder.isPasswordValid(bean.getDlmm(), dlmm);
	}

	public Dwdm login(String dlmc, String dlmm, HttpServletRequest request,
			HttpServletResponse response, SessionProvider session)
			throws UsernameNotFoundException, BadCredentialsException {
		Dwdm bean = getByDlmc(dlmc);
		if (bean == null) {
			throw new UsernameNotFoundException("登录名不存在: " + dlmc);
		}
		if (!pwdEncoder.isPasswordValid(bean.getDlmm(), dlmm)) {
			throw new BadCredentialsException("登录密码无效");
		}
		session.setAttribute(request, response, SysConstants.DWDM_KEY, bean);
		return bean;
	}

	public Dwdm resetDlmm(String dwdmid) {
		Dwdm dwdm = findById(dwdmid);
		dwdm.setDlmm(pwdEncoder.encodePassword(SysConstants.PASSWORD));
		return dwdm;
	}

	public Dwdm save(Dwdm dwdm) {
		return dao.save(dwdm);
	}

	@Override
	public Dwdm update(Updater<Dwdm> updater) {
		return dao.updateByUpdater(updater);
	}

}
